<template>
  <div id="app" class="app">
    <router-view @login="loginDirect" @logout="logoutDirect"></router-view>
  </div>
</template>

<script>
  export default {
    name: "app",
    created () {
      console.log("app创建");
      this.sign();
    },
    methods: {
      sign(callback){
        let vm = this;
        let localUser = localStorage.getItem("token");
        if(!localUser){
          return this.$router.push({
            path: "/login",
            query: {
              // from: this.$route.path
              from: vm.$router.currentRoute.path
            }
          });
        }
        typeof callback == "function" && callback();
      },
      loginDirect(newPath){
        this.sign(() => {
          this.$router.replace({
            path: newPath || "/index/home"
          });
        });
      },
      logoutDirect(){
        localStorage.user = "";
        localStorage.menus = "";
        localStorage.token = "";
        this.$cookies.remove();
        this.$store.commit("clear_tabs");
        this.$router.replace("/login");
      }
    }
  }
</script>

<style lang="less">
@import './assets/css/font-awesome.min.css';
@import './assets/css/app.css';
</style>
